// miniprogram/pages/ShopAdmin/OrderDetails/OrderDetails.js
const db = wx.cloud.database()
const cont = db.collection('Order')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Orders:[],
    showModal:false,
    ExpressNumber:''
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  ckwl:function(e){
    var OrderId=this.data.Orders[0].id
    var CourierNumber=this.data.Orders[0].CourierNumber
    wx.navigateTo({
      url: "/pages/express/express?OrderId=" + OrderId +"&CourierNumber="+CourierNumber,
    })
  },
  getOrder:function(OrderId){
    var that=this
    wx.showLoading({
      title: '加载中',
    })
    cont.doc(OrderId).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        var id = "Orders[0].id"
          var Consignee = "Orders[0].Consignee"
          var DeliveryTime = "Orders[0].DeliveryTime"
          var GoodsId = "Orders[0].GoodsId"
          var ReceivingTime = "Orders[0].ReceivingTime"
          var CreationTime = "Orders[0].CreationTime"
          var PaymentTime = "Orders[0].PaymentTime"
          var Remarks = "Orders[0].Remarks"
          var SellOpenid = "Orders[0].SellOpenid"
          var Telephone = "Orders[0].Telephone"
          var openid = "Orders[0].openid"
          var address = "Orders[0].address"
          var color = "Orders[0].color"
          var number = "Orders[0].number"
          var rmb = "Orders[0].rmb"
          var rmb2 = "Orders[0].rmb2"
          var size = "Orders[0].size"
          var state = "Orders[0].state"
          var GoodsTitle = "Orders[0].GoodsTitle"
          var SellNickName = "Orders[0].SellNickName"
          var SellAvatarUrl = "Orders[0].SellAvatarUrl"
          var GoodsImg = "Orders[0].GoodsImg"
          var express = "Orders[0].express"
          var CourierNumber="Orders[0].CourierNumber"
          
          that.setData({
            [id] :res.data._id,
            [Consignee]:res.data.Consignee,
            [DeliveryTime]:res.data.DeliveryTime,
            [GoodsId]:res.data.GoodsId,
            [ReceivingTime]:res.data.ReceivingTime,
            [CreationTime]:res.data.CreationTime,
            [PaymentTime]:res.data.PaymentTime,
            [Remarks]:res.data.Remarks,
            [SellOpenid]:res.data.SellOpenid,
            [Telephone]:res.data.Telephone,
            [openid]:res.data._openid,
            [address]:res.data.address,
            [color]:res.data.color,
            [number]:res.data.number,
            [rmb]:res.data.rmb.toFixed(2),
            [rmb2]:(res.data.rmb*res.data.number+res.data.express).toFixed(2),
            [size]:res.data.size,
            [state]:res.data.state,
            [GoodsTitle]:res.data.GoodsTitle,
            [SellNickName]:res.data.SellNickName,
            [SellAvatarUrl]:res.data.SellAvatarUrl,
            [GoodsImg]:res.data.GoodsImg,
            [express]:res.data.express,
            [CourierNumber]:res.data.CourierNumber,
          })
          wx.hideLoading();
      })
      .catch(err => {
        wx.hideLoading();
        console.error(err)
    })
  },
  onLoad: function (options) {
    console.log(options.OrderId)
    this.getOrder(options.OrderId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
/**
  * 弹窗
*/
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  //获取当前时间
  getNowDatTime:function(){
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate<=9) {
      strDate = "0" + strDate;
    }
    var time = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
    console.log(time);
    return time;
  },
  // 获取输入内容
  inputChange: function (e) {
    this.setData({
      ExpressNumber: e.detail.value
    })
  },
  
  /**
    * 隐藏模态对话框
  */
  hideModal: function () {
    this.setData({
      showModal: false,
      ExpressNumber: '',
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that=this
    console.log(that.data.Orders[0].id)
    if(that.data.ExpressNumber!=''){
      wx.showLoading({
        title: '发货中',
      })
      wx.cloud.callFunction({
        name: "DeliverGoods",
        data: {
          OrderId: that.data.Orders[0].id,
          CourierNumber:that.data.ExpressNumber,
          state:"待收货",
          DeliveryTime:this.getNowDatTime()
        }
      }).then(res => {
        console.log(res.data);
        wx.hideLoading();
        this.hideModal();
        wx.showToast({
          title: "发货成功",
          icon: "none"
        })
        setTimeout(function () {
          that.return()
        }, 1000) 
      }).catch(err => {
        console.log(err);
        wx.hideLoading();
        wx.showToast({
          title: "发货失败",
          icon: "none"
        })
      })
    }else{
      wx.showToast({
        title: "单号不能为空",
        icon: "none"
      })
    }
    
  },
    
})